import { Injectable, LoggerService, Inject } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';

@Injectable()
export class LogService implements LoggerService {
  constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {}

  log(message: string, context?: string) {
    this.logger.info(message, { context });
  }

  error(message: string, trace?: string, context?: string) {
    this.logger.error(message, { trace, context });
  }

  warn(message: string, context?: string) {
    this.logger.warn(message, { context });
  }

  debug(message: string, context?: string) {
    this.logger.debug(message, { context });
  }

  verbose(message: string, context?: string) {
    this.logger.verbose(message, { context });
  }

  // 自定义方法用于记录业务日志
  info(message: string, meta?: any) {
    this.logger.info(message, meta);
  }

  // 记录操作日志
  operation(operation: string, userId?: string, details?: any) {
    this.logger.info(`Operation: ${operation}`, {
      userId,
      details,
      timestamp: new Date().toISOString(),
    });
  }

  // 记录数据变更
  dataChange(table: string, action: string, data?: any) {
    this.logger.info(`Data Change: ${table}.${action}`, {
      table,
      action,
      data,
      timestamp: new Date().toISOString(),
    });
  }
}
